package com.eclink.asset.controller;

import com.eclink.asset.convert.AssetCategoryConvert;
import com.eclink.asset.domain.AssetCategory;
import com.eclink.asset.pojo.dto.AssetCategoryDTO;
import com.eclink.asset.pojo.query.AssetCategoryQuery;
import com.eclink.asset.pojo.vo.AssetCategoryVO;
import com.eclink.asset.service.AssetCategoryService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * 资产分类表Controller
 *
 * @author zcc
 * @date 2024-05-21
 */
@Api(description ="资产分类表接口列表")
@RestController
@RequestMapping("/wms/assetCategory")
public class AssetCategoryController extends BaseController {
    @Autowired
    private AssetCategoryService service;
    @Autowired
    private AssetCategoryConvert convert;

    @ApiOperation("查询资产分类表列表")
    @PostMapping("/list")
    public ResponseEntity<Page<AssetCategory>> list(@RequestBody AssetCategoryQuery query, Pageable page) {
        List<AssetCategory> list = service.selectList(query, page);
        return ResponseEntity.ok(new PageImpl<>(list, page, ((com.github.pagehelper.Page)list).getTotal()));
    }

    @ApiOperation("查询资产分类表列表")
    @GetMapping("/listAll")
    public ResponseEntity<List<AssetCategory>> listAll() {
        List<AssetCategory> list = service.selectListAll();
        return ResponseEntity.ok(list);
    }

    @ApiOperation("查询资产分类表的树节点")
    @GetMapping("/getTreeNodesByParentId")
    public ResponseEntity<List<AssetCategory>> getTreeNodesByParentId(String parentId) {
        List<AssetCategory> list = service.getListByParentId(parentId);
        return ResponseEntity.ok(list);
    }

    @ApiOperation("导出资产分类表列表")
    @Log(title = "资产分类表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ResponseEntity<String> export(AssetCategoryQuery query) {
        List<AssetCategory> list = service.selectList(query, null);
        ExcelUtil<AssetCategoryVO> util = new ExcelUtil<>(AssetCategoryVO.class);
        return ResponseEntity.ok(util.writeExcel(convert.dos2vos(list), "资产分类表数据"));
    }

    @ApiOperation("获取资产分类表详细信息")
    @GetMapping(value = "/{id}")
    public ResponseEntity<AssetCategory> getInfo(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(service.selectById(id));
    }

    @ApiOperation("新增资产分类表")
    @Log(title = "资产分类表", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseEntity<Integer> add(@RequestBody AssetCategoryDTO assetCategory) {

        String parentNumber = assetCategory.getParentNumber();
        Integer parentId = assetCategory.getParentId();
        String number = service.getLastNumberFormat(parentId, parentNumber);
        AssetCategory dataObject = convert.dto2do(assetCategory);
        dataObject.setNumber(number);

        return ResponseEntity.ok(service.insert(dataObject));
    }

    @ApiOperation("修改资产分类表")
    @Log(title = "资产分类表", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseEntity<Integer> edit(@RequestBody AssetCategory assetCategory) {
        return ResponseEntity.ok(service.update(assetCategory));
    }

    @ApiOperation("删除资产分类表")
    @Log(title = "资产分类表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public ResponseEntity<Integer> remove(@PathVariable Long[] ids) {
        return ResponseEntity.ok(service.deleteByIds(ids));
    }
}
